package im

import (
    //"fmt"
    //"errors"
    //"reflect"
	"dhfshop/controllers/base"
	"encoding/json"
	"thirdpaysdk/api/gpslmall"
	"dhfshop/domain/im"
	"dhfshop/domain/pay"
	"github.com/astaxie/beego"
	"dhfshop/service/redisgo"
)

type GpslNotifyController struct {
	base.BaseController
}

func (this *GpslNotifyController) GpslPayNotify() {
	notifyData := this.Ctx.Input.RequestBody
	beego.Debug("GpslPayNotify notifyData:", string(notifyData))
	//
	var response gpslmall.Notify
	err := json.Unmarshal(notifyData, &response)
	if err != nil {
		beego.Debug("GpslPayNotify:", err.Error())
		return
	}
	var payInfoCache *paylayer.PayInfoCache = new(paylayer.PayInfoCache)
	cacheKey := "payorder_" + response.MerchantOrderNo
	redisconn := redisgo.GetInstance()
	if cacheValue, err := redisconn.GetString(cacheKey); err == nil && len(cacheValue) > 5  {
	    //beego.Debug("cache pay info:", cacheValue)
		if err = json.Unmarshal([]byte(cacheValue), payInfoCache); err == nil {
			
		}
	}else{
	    beego.Debug("cache pay info err:", err.Error())
		return 
	}
	
	reqMap := make(map[string]string)
	reqMap["shopCode"] = "GPSL_PAY_NOTIFY"
	reqMap["merchantOrderNo"] = response.MerchantOrderNo
	//reqMap["amount"] = response.
	reqMap["transactionNo"] = response.TransactionNo
	reqMap["time"] = response.PayTime
	reqMap["status"] = response.Status
	reqMap["desc"] = response.StatusDesc
	reqMap["LocalKind"] = "GPSL_PAY_NOTIFY"
	reqMap["attach"] = payInfoCache.OrderKind
	imlayer.PushMsgSvrIns.PushDao <- &reqMap
	this.payReturnMessage("SUCCESS", "")  // 务必回复
	return
}

func (this *GpslNotifyController) payReturnMessage(code, msg string) {
	asyNotifyRsp := map[string]interface{}{
		"returnCode": code,
		"return_msg":  msg,
	}
	wxRspJson, _:= json.Marshal(asyNotifyRsp)
	this.Ctx.WriteString(string(wxRspJson))
}


